#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
 struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 };
 
class Solution {
public:
    bool A(TreeNode* pRoot, int* n)
    {
        if (!pRoot)
        {
            *n = 0;
            return true;
        }
        int ln = 0, rn = 0;
        bool r1 = A(pRoot->left, &ln);
        bool r2 = A(pRoot->right, &rn);
        if (!r1 || !r2)
            return false;
        cerr << pRoot->val << ",ln=" << ln << ",rn=" << rn << endl;
        *n = max(ln + 1, rn + 1);
        return (int)abs(ln - rn) <= 1;
    }
    void B(TreeNode* cur, int n)
    {
        if (!cur)
        {
            cerr << "null,n=" << n << endl;
            return;
        }
        B(cur->left, n + 1);
        B(cur->right, n + 1);
        cerr << cur->val << ",n=" << n << endl;
        return;
    }
    bool IsBalanced_Solution(TreeNode* pRoot)
    {
        auto cur = pRoot;
        //B(cur,1);
        int n = 0;
        auto ret = A(pRoot, &n);
        cerr << ret << endl;
        return ret;
    }
};